package com.learn.lessons.helloworld.designModes.strategy;

import com.learn.lessons.helloworld.commons.FileTypeResolveEnum;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @BelongsProject: HelloWorld
 * @BelongsPackage: com.learn.lessons.helloworld.designModes
 * @Author: guodf
 * @CreateTime: 2022-11-03  13:48
 * @Description:
 * @Version: 1.0.0
 */
@Component
public class StrategyUseService implements ApplicationContextAware {

    private final Map<FileTypeResolveEnum, FileResolveStrategy> fileStrategy = new ConcurrentHashMap<>();

    public void resolveFile(FileTypeResolveEnum fileTypeResolveEnum, Object objectParam) {
        FileResolveStrategy iFileStrategy = fileStrategy.get(fileTypeResolveEnum);
        if (iFileStrategy != null) {
            iFileStrategy.resolve(objectParam);
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, FileResolveStrategy> tempMap = applicationContext.getBeansOfType(FileResolveStrategy.class);
        tempMap.values().forEach(strategyService -> fileStrategy.put(strategyService.gainFileType(), strategyService));
        System.out.println("11111111111111111");
    }
}
